package com.yuanwushe.blue.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuanwushe.blue.models.Msg;
import com.yuanwushe.blue.onceandroid.R;

import java.util.List;

/**
 * Created by blue on 2018/5/28.
 */

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.MsgHolder> {

    List<Msg> mMsgList;
    public MsgAdapter(List<Msg> msgList){
        mMsgList = msgList;
    }
    @Override
    public MsgHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_item, parent, false);
        return new MsgHolder(view);
    }

    @Override
    public void onBindViewHolder(MsgHolder holder, int position) {
        Msg msg = mMsgList.get(position);
        if (msg.getType() == Msg.TYPE_RECRIVED){
            holder.LeftLayout.setVisibility(View.VISIBLE);
            holder.RightLayout.setVisibility(View.GONE);
            holder.LeftText.setText(msg.getContent());
        } else if (msg.getType() == Msg.TYPE_SENT) {
            holder.RightLayout.setVisibility(View.VISIBLE);
            holder.LeftLayout.setVisibility(View.GONE);
            holder.RightText.setText(msg.getContent());
        }
    }

    @Override
    public int getItemCount() {
        return mMsgList.size();
    }

    public class MsgHolder extends RecyclerView.ViewHolder{
        LinearLayout LeftLayout;
        LinearLayout RightLayout;
        TextView LeftText;
        TextView RightText;
        public MsgHolder(View itemView) {
            super(itemView);
            LeftLayout = itemView.findViewById(R.id.left_layout);
            RightLayout = itemView.findViewById(R.id.right_layout);
            LeftText = itemView.findViewById(R.id.left_msg);
            RightText = itemView.findViewById(R.id.right_msg);
        }
    }
}
